﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace System.Threading
{
    /// <summary>
    /// 
    /// </summary>
   public class CountDownLatch
    {
        private object lockObj = new Object();
        private int counter;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="counter"></param>
        public CountDownLatch(int counter)
        {
            this.counter = counter;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Await()
        {
            lock (lockObj)
            {
                while (counter > 0)
                {
                    Monitor.Wait(lockObj);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void CountDown()
        {
            lock (lockObj)
            {
                counter--;
                Monitor.PulseAll(lockObj);
            }
        }
    }
}
